#import "CTLocalizedManager.h"
#import "CTLANManager.h"
@interface CTLocalizedManager ()
@property (nonatomic, strong) NSDictionary *pathDic;
@property (nonatomic, strong) NSDictionary *paramDic;
@end
@implementation CTLocalizedManager
+ (instancetype)shareManager {
    static CTLocalizedManager *instance ;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        instance = [[super allocWithZone:NULL] init];
        [instance localPath];
    });
    return instance;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
    return [CTLocalizedManager shareManager];
}
- (id)copyWithZone:(struct _NSZone *)zone {
    return [CTLocalizedManager shareManager];
}
- (void)localPath {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"LocalizablePath" ofType:@"json"];
    NSData *jsonData = [[NSData alloc] initWithContentsOfFile:path];
    self.pathDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
}
- (void)localParam { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"LocalizableParameter" ofType:@"json"];
    NSData *jsonData = [[NSData alloc] initWithContentsOfFile:path];
    self.paramDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
}
#pragma mark - get url
- (NSString *)localizedParameterWithKey:(NSString *)key {
    return (_paramDic == nil||!_paramDic[key]) ? @"" :_paramDic[key];
}
- (NSString *)localizedPathWithKey:(NSString *)key withType:(CTPathType)type withData:(nullable id)data {
    NSString *path = (_pathDic == nil||!_pathDic[key]) ? key :_pathDic[key];
    if (type == CTPathTypePost) {
        NSString *fullPostUrl = [self fullPathForPost:path withData:data];
        return fullPostUrl;
    } else if (type == CTPathTypeReport) {
        NSString *fullReportUrl = [self fullPathForReport:path withData:data];
        return fullReportUrl;
    } else if (type == CTPathTypeCredit) {
        NSString *fullUrl = [self fullPathForCredit:path withData:data];
        return fullUrl;
    } else if (type == CTPathTypeWeb) {
        NSString *fullWebUrl = [self fullPathForWeb:key];
        return fullWebUrl;
    }
    else {
        return @"";
    }
}
- (NSString *)fullPathForPost:(NSString *)pathString withData:(nullable id)data {
    NSString *basePath = [CTEnvironmentManager shareManager].baseUrlPath;
    NSString *normalPath = [CTEnvironmentManager shareManager].normalPath;
    NSString *urlStr = [NSString stringWithFormat:@"%@%@%@", basePath, normalPath, pathString];
    NSString *fullUrl = [self appendOtherParam:urlStr withData:data];
    return fullUrl;
}
- (NSString *)fullPathForCredit:(NSString *)pathString withData:(nullable id)data {
    NSString *basePath = [CTEnvironmentManager shareManager].baseUrlPath;
    NSString *creditPath = [CTEnvironmentManager shareManager].creditPath;
    NSString *urlStr = [NSString stringWithFormat:@"%@%@%@", basePath, creditPath, pathString];
    NSString *fullUrl = [self appendOtherParam:urlStr withData:data];
    return fullUrl;
}
- (NSString *)fullPathForReport:(NSString *)pathString withData:(nullable id)data {
    NSString *basePath = [CTEnvironmentManager shareManager].baseUrlPath;
    NSString *reportPath = [CTEnvironmentManager shareManager].reportPath;
    NSString *urlStr = [NSString stringWithFormat:@"%@%@%@", basePath, reportPath, pathString];
    NSString *fullUrl = [self appendOtherParam:urlStr withData:data];
    return fullUrl;
}
- (NSString *)fullPathForWeb:(NSString *)pathString {
    NSString *baseUrl = [CTEnvironmentManager shareManager].environmentH5Url;
    NSString *urlStr = [NSString stringWithFormat:@"%@%@", baseUrl, pathString];
    NSString *fullUrl = [self appendWebOtherParam:urlStr];
    return fullUrl;
}
- (NSString *)appendWebOtherParam:(NSString *)string {
    BOOL isLaunchWeb = [[NSUserDefaults standardUserDefaults] boolForKey:kIsLaunchWeb];
    NSString *urlString = [NSString stringWithFormat:@"%@?pagetype=2&ch=%@&cid=%@&appver=nigeria_%@&mainver=%@&c=%@&b=%@&t=%@&lan=%@&isH5=%d", string, [CTAFManager getChannel], [CTUserManager ct_getCid], App_Version, App_Version, [CTEnvironmentManager shareManager].channel, [CTEnvironmentManager shareManager].business, kGetCurentTime, [CTLANManager getWebLan], isLaunchWeb];
    CTUserModel *model = [CTUserManager shareManager].userModel;
    if (model.userEntityId != 0) {
        urlString = [urlString stringByAppendingFormat:@"&u=%ld", (long)[CTUserManager shareManager].userModel.userEntityId];
    }
    urlString = [urlString stringByAppendingString:@"#/entry"];
    return urlString;
}
- (NSString *)appendOtherParam:(NSString *)string withData:(nullable id)data {
    NSString *urlString = [NSString stringWithFormat:@"%@?ek=1&conditionalIndicator=%@&binaryIndicator=%@&booleanLogic=%@", string,  [CTEnvironmentManager shareManager].channel, [CTEnvironmentManager shareManager].business, [CTLANManager getLocal]];
    CTUserModel *model = [CTUserManager shareManager].userModel;
    if (model.userEntityId != 0) {
        urlString = [urlString stringByAppendingFormat:@"&uniqueFlag=%ld", (long)[CTUserManager shareManager].userModel.userEntityId];
    }
    if (!beNil(model.userAuthenticationToken)) {
        urlString = [urlString stringByAppendingFormat:@"&temporalIndicator=%@", [CTUserManager shareManager].userModel.userAuthenticationToken];
    }
    urlString = [urlString stringByAppendingFormat:@"&applicationUniqueIdentifier=%@", [CTEnvironmentManager shareManager].appid];
    urlString = [urlString stringByAppendingFormat:@"&uniqueZuid=%@", [CTUserManager ct_getZuid]];
    urlString = [urlString stringByAppendingFormat:@"&countryAbbreviation=%@", [CTAFManager getChannel]];
    if ([CTEnvironmentManager shareManager].environmentType == 1) {
        urlString = [urlString stringByAppendingFormat:@"&%@", isEncrypt]; 
        if (data != nil) {
            NSDictionary *parametersDic = (NSDictionary *)data;
            for (NSString *key in parametersDic) {
                urlString = [urlString stringByAppendingFormat:@"&%@=%@", key, parametersDic[key]];
            }
        }
        urlString = [NSString ct_encodeUrl:urlString];
    } else {
        if (data != nil) {
            NSDictionary *parametersDic = (NSDictionary *)data;
            for (NSString *key in parametersDic) {
                urlString = [urlString stringByAppendingFormat:@"&%@=%@", key, parametersDic[key]];
            }
        }
    }
    return urlString;
}
@end
